<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Title</title>
<head>
</head>
<style>
    .layui-form-selectup dl {
        top: 42px;
        bottom: unset;
    }
</style>
<body>
</body>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">招聘推荐</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>招聘推荐</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
<!--            搜索：-->
<!--            <select id="industry" name="industry" lay-filter="industry">-->
<!--                <option value="" style="margin-top: 50px;">行业名-</option>-->
<!--            </select>-->
<!--            <select id="position" name="position" lay-filter="position">-->
<!--                <option value="">职位名-</option>-->
<!--            </select>-->
<!--            <select id="employedStatus" name="employedStatus" lay-filter="employedStatus">-->
<!--                <option value="">就业状态-</option>-->
<!--                <option value="0">待业中</option>-->
<!--                <option value="1">已就业</option>-->
<!--            </select>-->
            <!--            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="职位名"/>&emsp;-->
<!--            <button id="user-btn-search" class="layui-btn icon-btn"><i-->
<!--                    class="layui-icon">&#xe615;</i>搜索-->
<!--            </button>-->
            <!--            <button id="user-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
            <button id="user-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="recommend-table" lay-filter="recommend-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="recommend-table-bar">
    <a style="background-color: #00b83f;" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">内推</a>
<!--    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>-->
</script>

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-state">
    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>


    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var formSelects = layui.formSelects;


        // 手动联动行业&&职位
        let pid = 0;
        let pidd = JSON.stringify(pid);
        // 默认查询行业
        $.ajax({
            url: config.base_server + 'api-recruitment/industry/findIndustry',
            // data:JSON.stringify({pid:pid}),
            data: JSON.stringify(pid),
            type: 'post',
            contentType: 'application/json',
            beforeSend: function (xhr) {
                let token = config.getToken()
                if (token) {
                    // xhr.setRequestHeader('Authorization', 'Basic ' + token.access_token);
                    xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                }
            },
            dataType: 'JSON',
            async: false,
            success: function (res) {
                for (let i = 0; i < res.data.length; i++) {
                    var industryName = res.data[i].industryName;
                    var value = res.data[i].id;
                    // 使用循环遍历，给下拉列表赋值
                    $('#industry').append(new Option(industryName, value));
                }
            }
        })
        form.render();
        // 默认查询职位
        layui.use(['layer', 'jquery', 'form'], function () {
            var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form;
            form.on('select(industry)', function (data) {
                var pid = $("#industry").val();

                $.ajax({
                    url: config.base_server + 'api-recruitment/industry/findPosition',
                    // data:JSON.stringify({pid:pid}),
                    data: JSON.stringify(pid),
                    type: 'post',
                    contentType: 'application/json',
                    beforeSend: function (xhr) {
                        let token = config.getToken()
                        if (token) {
                            xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                        }
                    },
                    dataType: 'JSON',
                    async: false,
                    success: function (res) {
                        for (let i = 0; i < res.data.length; i++) {
                            var industryName = res.data[i].industryName;
                            var value = res.data[i].id;
                            // 使用循环遍历，给下拉列表赋值
                            $('#position').append(new Option(industryName, value));
                        }
                    }
                })
                form.render();
            });
        });

        // 添加按钮绑定权限
        if (!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-add').hide();
        }

        // 导出按钮绑定权限
        if (!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-export').hide();
        }

        // 招聘推荐相关信息 渲染表格
        table.render({
            elem: '#recommend-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-recruitment/recommend/findJobOffers',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            // headers: {
            //    Authorization:'Bearer '+ config.getToken().access_token
            //    page:0,
            //    limit:10
            // },
            // parseData: function (res) {
            //     console.log("sbbbb" + res.data)
            //     return {
            //         "msg": res.message, //解析提示文本
            //         "count": res.count, //解析数据长度
            //         "data": res.data //解析数据列表
            //     };
            // },
            page: true,
            // response: {
            //     statusCode: 0,
            // },
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '招聘编号'},
                {field: 'companyId', sort: true, title: '公司编号'},
                {field: 'companyname', sort: true, title: '公司名称'},
                {field: 'skills', sort: true, title: '技能要求'},
                {field: 'employeeCount', sort: true, title: '限招人数/名'},
                {field: 'exprience', sort: true, title: '经验要求/年'},
                {field: 'tasks', sort: true, title: '任务内容'},
                {field: 'remark', sort: true, title: '职位名称'},
                {field: 'positionCost', sort: true, title: '薪资/K'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.startTime);
                    }, title: '开始时间'
                },
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.endTime);
                    }, title: '截止时间'
                },
                // {field: 'enabled', sort: true, templet: '#user-tpl-state', title: '状态', width: 150},
                {align: 'center', toolbar: '#recommend-table-bar', title: '操作', width: 150}
            ]]
        });

        // // 添加按钮点击事件
        $('#user-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(recommend-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
            /*
            *  else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/users/' + obj.data.id + '/resetPassword', {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            }
            * */
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            var s = JSON.stringify(data);
            // alert(s);
            //修改sex值
            // if (data) {
            //     data.sex = data.sex + '';
            // }
            admin.putTempData('t_user', data);
            var title = data ? '待业人员内推' : '选择内推';
            admin.popupCenter({
                title: title,
                path: 'pages/recruitment/recruitment_form.html',
                finish: function () {
                    table.reload('recommend-table', {});
                }
            });
        };


        // 无条件直接导出
        $('#user-btn-export').click(function () {
            var url = config.base_server + 'api-recruitment/industry/exportUnemployed?access_token=' + config.getToken().access_token;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });

        // 修改user状态
        form.on('switch(user-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/users/updateEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });
</script>
</html>